package com.example.broadcastwebsocket.controller;

import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/stream")
public class StreamController {

    @GetMapping(produces = MediaType.APPLICATION_XML_VALUE)
    public ResponseEntity<byte[]> getStream() throws IOException {
        ClassPathResource resource = new ClassPathResource("static/sample.mpd");
        File file = resource.getFile();
        byte[] content = org.apache.commons.io.FileUtils.readFileToByteArray(file);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_XML);
        headers.setContentLength(content.length);

        return new ResponseEntity<>(content, headers, HttpStatus.OK);
    }
}